package com.ecloud.service.security.config;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.Rows;
import com.ecloud.common.base.translate.TranslateSource;
import com.ecloud.common.utils.check.CollectionUtils;
import com.ecloud.common.web.jackson.TranslateSerializerModifier;
import com.ecloud.common.web.translate.CustomSerializeFactory;
import com.ecloud.common.web.translate.ICustomSerialize;
import com.ecloud.openapi.translate.dto.BaseDictionaryDto;
import com.ecloud.openapi.translate.dto.BaseTranslateInfoDto;
import com.ecloud.openapi.translate.vo.BaseDictionaryListVo;
import com.ecloud.openapi.translate.vo.BaseTranslateInfoListVo;
import com.ecloud.service.security.feign.iface.BaseDictionaryFeign;
import com.ecloud.service.security.feign.iface.BaseTranslateInfoFeign;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 翻译配置<br>
 *
 * @author LiuGangQiang Create in 2020/03/02
 */
@Configuration
public class TranslateConfiguration {
    @Resource
    private BaseDictionaryFeign dictionaryFeign;
    @Resource
    private BaseTranslateInfoFeign translateFeign;

    /**
     * 字典表翻译服务
     *
     * @author LiuGangQiang Create in 2021/08/19
     * @return {@link ICustomSerialize}
     */
    private ICustomSerialize dictionarySerialize() {
        return new ICustomSerialize() {
            @Override
            public String serialize(String type, String source) {
                BaseDictionaryDto dto = new BaseDictionaryDto();
                dto.setType(type);
                dto.setCode(source);
                dto.setLanguage(LocaleContextHolder.getLocale().toLanguageTag());
                Result<Rows<BaseDictionaryListVo>> result = dictionaryFeign.queryList(dto);
                if (result.compare(ResultStatus.OK)) {
                    List<BaseDictionaryListVo> dicts = result.getData().getRows();
                    if (CollectionUtils.isGtZero(dicts)) {
                        for (BaseDictionaryListVo vo : dicts) {
                            if (vo.getCode().equals(source)) {
                                return vo.getContent();
                            }
                        }
                    }
                }
                return source;
            }
        };
    }

    /**
     * 翻译表翻译服务
     *
     * @author LiuGangQiang Create in 2021/08/19
     * @return {@link ICustomSerialize}
     */
    public ICustomSerialize translateSerialize() {
        return new ICustomSerialize() {
            @Override
            public String serialize(String type, String source) {
                BaseTranslateInfoDto dto = new BaseTranslateInfoDto();
                dto.setType(type);
                dto.setSource(source);
                dto.setLanguage(LocaleContextHolder.getLocale().toLanguageTag());
                Result<Rows<BaseTranslateInfoListVo>> result = translateFeign.queryList(dto);
                if (result.compare(ResultStatus.OK)) {
                    List<BaseTranslateInfoListVo> tranlates = result.getData().getRows();
                    if (CollectionUtils.isGtZero(tranlates)) {
                        for (BaseTranslateInfoListVo vo : tranlates) {
                            if (vo.getSource().equals(source)) {
                                return vo.getTarget();
                            }
                        }
                    }
                }
                return source;
            }
        };
    }

    /**
     * 实例化自定义序列化工厂
     *
     * @author LiuGangQiang Create in 2021/08/19
     * @return {@link CustomSerializeFactory}
     */
    @Bean
    public CustomSerializeFactory customSerializeFactory() {
        CustomSerializeFactory factory = new CustomSerializeFactory();
        factory.register(TranslateSource.DICT, dictionarySerialize());
        factory.register(TranslateSource.TRANSLATE, translateSerialize());
        return factory;
    }

    /**
     * 配置框架序列化
     *
     * @author LiuGangQiang Create in 2021/08/19
     * @param builder 序列化构建者
     * @return {@link ObjectMapper}
     */
    @Bean
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        objectMapper.setSerializerFactory(objectMapper.getSerializerFactory()
                .withSerializerModifier(new TranslateSerializerModifier(customSerializeFactory())));
        return objectMapper;
    }
}